`
https://leetcode.cn/problems/house-robber-iii/
`

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var rob = function (root) {
  const memo = new Map();

  function _rob(root) {
    if (root === null) {
      return 0;
    }
    // 利用备忘录消除重叠子问题
    if (memo.has(root)) {
      return memo.get(root);
    }
    // 抢，然后去下下家
    let do_it = root.val;
    if (root.left !== null) {
      do_it += _rob(root.left.left, memo) + _rob(root.left.right, memo);
    }
    if (root.right !== null) {
      do_it += _rob(root.right.left, memo) + _rob(root.right.right, memo);
    }
    // 不抢，然后去下家
    let not_do = _rob(root.left, memo) + _rob(root.right, memo);

    let res = Math.max(do_it, not_do);
    memo.set(root, res);
    return res;
  }

  return _rob(root);
};